var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
/**图片压缩变更方法 */
export const imageMin = (params = { quality: 1, fileName: '图片' }) => __awaiter(void 0, void 0, void 0, function* () {
    console.assert(params.file || params.url, '必须存在file或者url入参');
    const img = yield toImage({ file: params.file, url: params.url });
    const canvas = toCanvas(img, { size: params.size, maxLength: params.maxLength });
    const file = yield canvasToFile(canvas, { quality: params.quality, fileName: params.fileName });
    return file;
});
/**处理图片压缩，并且转成url */
export const imageMinByUrl = (params = { quality: 1 }) => __awaiter(void 0, void 0, void 0, function* () {
    console.assert(params.file || params.url, '必须存在file或者url入参');
    const img = yield toImage({ file: params.file, url: params.url });
    const canvas = toCanvas(img, { size: params.size, maxLength: params.maxLength });
    return canvas.toDataURL('image/png', params.quality);
});
/**转换成图片 */
function toImage(params) {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise((reso, rej) => {
            const { file, url } = params;
            const img = new Image();
            img.onload = () => {
                return reso(img);
            };
            // 文件方式
            if (file) {
                const reader = new FileReader();
                reader.onload = (e) => {
                    var _a;
                    img.setAttribute('src', (_a = e.target) === null || _a === void 0 ? void 0 : _a.result);
                };
                reader.readAsDataURL(file);
            }
            else if (url) {
                img.setAttribute('src', url);
            }
            else {
                console.error(`解析图片出错`);
            }
        });
    });
}
/**转换成canvas */
function toCanvas(img, options) {
    var _a;
    const canvas = document.createElement('canvas');
    let { size, maxLength } = options !== null && options !== void 0 ? options : {};
    let { height, width } = img;
    if (size || maxLength) {
        if ((!(size === null || size === void 0 ? void 0 : size.height) || !size.width) && !maxLength)
            maxLength = (_a = size === null || size === void 0 ? void 0 : size.height) !== null && _a !== void 0 ? _a : size === null || size === void 0 ? void 0 : size.width;
        // 设置长度并去适应
        if (maxLength) {
            let maxlen = Math.max(height, width);
            let rate = maxLength / maxlen;
            height = maxlen === height ? maxLength : height * rate;
            width = maxlen === width ? maxLength : width * rate;
        }
        else if (size) {
            // 设置了尺寸
            height = size.height;
            width = size.width;
        }
    }
    canvas.height = height;
    canvas.width = width;
    const ctv = canvas.getContext('2d');
    ctv === null || ctv === void 0 ? void 0 : ctv.drawImage(img, 0, 0, width, height);
    return canvas;
}
/**canvas转文件 */
function canvasToFile(canvas, options = { quality: 1, fileName: '图片' }) {
    return new Promise((reso, rej) => {
        canvas.toBlob((blob) => {
            const file = new File([blob], options.fileName, {
                type: 'image/png',
            });
            reso(file);
        }, '', options.quality);
    });
}
